//
//  NearUserCell.m
//  HotWord
//
//  Created by Rainbow on 8/19/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NearUserInfoCell.h"
#import "UserManager.h"

@implementation NearUserInfoCell
@synthesize backgroundView;
@synthesize headImageView;
@synthesize nickNameLabel;
@synthesize userInfoLabel;
@synthesize examLabel;
@synthesize statusLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (NSString *)getDistanceByLat:(NSString *)lat andLon:(NSString *)lon
{
    User *currentUser = [UserManager sharedManager].currentUser;
    if (!lat || !lon || !currentUser.latitude || !currentUser.longitude) {
        return @"unknow";
    }
    CLLocation *userLocation = [[[CLLocation alloc] initWithLatitude:[lat doubleValue]
                                                           longitude:[lon doubleValue]] autorelease];
    CLLocation *currentLocation = [[[CLLocation alloc] initWithLatitude:[currentUser.latitude doubleValue]
                                                              longitude:[currentUser.longitude doubleValue]] autorelease];
    CLLocationDistance distance = [currentLocation distanceFromLocation:userLocation];
    
    if (distance == 0) {
        return nil;
    }
    else {
        if (distance > 1000) {
            
            return [NSString stringWithFormat:@"%dkm",(NSUInteger)round(distance/1000.0)];
        }
        else {
            return [NSString stringWithFormat:@"%dm",(NSUInteger)round(distance)];
        }
        
    }
}

- (void)setUserForCell:(User *)user
{
    if (_user) {
        [_user release];
        _user = nil;
    }
    _user = [user retain];
    
    if (IS_STRING_NOT_EMPTY(_user.nickname))
    {
        self.nickNameLabel.text = _user.nickname;
    }
    else
    {
        self.nickNameLabel.text = @"昵称未知";
    }
    
    if (_user.status) {
        self.statusLabel.text = [NSString stringWithFormat:@"最近：%@",_user.status];
    }
    else {
        self.statusLabel.text = @"最近：这是个懒家伙！";
    }
    
    if (IS_STRING_NOT_EMPTY(_user.exam))
    {
        if ([@"Teacher" isEqualToString:_user.identity])
        {
            if (IS_STRING_NOT_EMPTY(_user.testSection))
            {
                self.examLabel.text = [NSString stringWithFormat:@"%@ | %@",_user.exam, _user.testSection,nil];
            }
            else
            {
                self.examLabel.text = _user.exam;
            }

        }
        else
        {
            self.examLabel.text = _user.exam;
        }
    }
    else
    {
        self.examLabel.text = @"";
    }


    NSString *age = @"";
    if (_user.year) {
        
        age = [NSString stringWithFormat:@"%@岁", _user.year];
    }
    else {
        age = @"年龄未知";
    }
    
    NSString *gender = @"男";
    if ([@"Girl" isEqualToString:user.gender]) {
        gender = @"女";
    }

    
    NSString *distanceStr = @"";
    CGFloat distance = [_user.distance floatValue];
    if (distance >= 1.0) {
        
        distanceStr = [NSString stringWithFormat:@"%0.1fkm", distance];
    }
    else if (distance < 0)
    {
        distanceStr = @"未知";
    }
    else
    {
        self.userInfoLabel.hidden = NO;
        distance = distance *1000;
        distanceStr = [NSString stringWithFormat:@"%0.0fm", distance];
    }
    
    NSString *identity = @"身份未知";

    if ([@"Student" isEqualToString:_user.identity])
    {
        identity = @"生";
    }
    else if ([@"Teacher" isEqualToString:_user.identity])
    {
        identity = @"师";
    }
    
    self.userInfoLabel.text = [NSString stringWithFormat:@"%@ | %@ | %@ | %@",distanceStr,gender,identity ,age];

    self.headImageView.showLoadingWithDefaultImage = YES;
    [self.headImageView setIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.headImageView setDefaultImage:[UIImage imageNamed:@"photo_card.png"]];
    [self.headImageView loadImage: _user.avater];
}

- (void)dealloc {
    [_user release];
    [backgroundView release];
    [headImageView release];
    [nickNameLabel release];
    [userInfoLabel release];
    [examLabel release];
    [statusLabel release];
    [super dealloc];
}
@end
